package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.McAccountInfoMapper;
import cc.rengu.igas.momp.common.dao.impl.McAccountInfoMapperImpl;
import cc.rengu.igas.momp.common.entity.TMAccountInfo;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.bean.CashierAccountInfoBean;
import cc.rengu.igas.momp.facade.request.QueryCashierInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryCashierInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 收银员信息查询
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/6/14 16:03
 */

public class QueryCashierInfoService extends RadpService {
    //收银员Id
    private String userId;
    //法人机构号
    private String globalInstId;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryCashierInfoRequest queryCashierInfoRequest = new QueryCashierInfoRequest();
            ConvertUtil.convertOutput(queryCashierInfoRequest);

            QueryCashierInfoResponse queryCashierInfoResponse = new QueryCashierInfoResponse();
            queryCashierInfoResponse.setHeader(queryCashierInfoRequest.getHeader());
            BizResponse<QueryCashierInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryCashierInfoResponse);

            String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            userId = queryCashierInfoRequest.getUserId();
            globalInstId = queryCashierInfoRequest.getHeader().getInstId();
            /* 检查操作员机构标示 */
            if (StringUtil.isNullorEmpty(userId)) {
                rglog.error("收银员ID为空!");
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            if (StringUtil.isNullorEmpty(globalInstId)) {
                rglog.error("法人机构号为空!");
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }

            // 服务调用
            BizResponse<QueryCashierInfoResponse> bizResponseNew = queryCashierInfo(xmlTreeUtil, queryCashierInfoRequest, queryCashierInfoResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 收银员信息查询
     *
     * @param xmlTreeUtil              内部XML树
     * @param queryCashierInfoRequest  收银员信息查询接口请求报文
     * @param queryCashierInfoResponse 收银员信息查询接口应答报文
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.CashierAccountManageResponse>收银员信息查询接口应答报文
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 14:50
     */

    private BizResponse<QueryCashierInfoResponse> queryCashierInfo(XmlTreeUtil xmlTreeUtil, QueryCashierInfoRequest queryCashierInfoRequest, QueryCashierInfoResponse queryCashierInfoResponse) throws Exception {

        BizResponse<QueryCashierInfoResponse> bizResponse = new BizResponse<>();
        /* 先查询系统参数表 */
        McAccountInfoMapper mcAccountInfoMapper = new McAccountInfoMapperImpl();
        TMAccountInfo tmAccountInfo = mcAccountInfoMapper.selectMcAccountInfoByInstIdUserId(globalInstId, userId);
        if (null == tmAccountInfo) {
            rglog.error("根据法人机构号<{}>收银员Id<{}>查询收银员信息表的不存在!", globalInstId, userId);
            throw new BizException(RespCodeEnum.ACCOUNT_INFO_EXISTENCE.getRespCode(), RespCodeEnum.ACCOUNT_INFO_EXISTENCE.getRespDesc());
        }
        CashierAccountInfoBean cashierAccountInfoBean = convertAccountInfo(tmAccountInfo);
        queryCashierInfoResponse.setAcctInfo(cashierAccountInfoBean);
        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        queryCashierInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryCashierInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryCashierInfoResponse);

        return bizResponse;
    }

    /**
     * @param tmAccountInfo 收银员信息实体类
     * @return
     * @throws BizException
     */
    private CashierAccountInfoBean convertAccountInfo(TMAccountInfo tmAccountInfo) throws BizException {
        CashierAccountInfoBean cashierAccountInfoBean = new CashierAccountInfoBean();
        /** 用户账号*/
        cashierAccountInfoBean.setAcctNo(tmAccountInfo.getUserAcct());
        /**账号名称*/
        cashierAccountInfoBean.setAcctName(tmAccountInfo.getUserName());
        /**证件类型*/
        cashierAccountInfoBean.setIdType(tmAccountInfo.getCertType());
        /**证件号码*/
        cashierAccountInfoBean.setIdNo(tmAccountInfo.getCertNo());
        /**预留手机号*/
        cashierAccountInfoBean.setCellPhoneNumber(tmAccountInfo.getMobileNo());
        return cashierAccountInfoBean;
    }
}
